from odoo import api, fields, models


class AgriCrop(models.Model):
    _name = 'agri.crop'
    _description = '作物/花木'
    _inherit = ['mail.thread', 'mail.activity.mixin']
    _rec_name = 'display_name'

    name = fields.Char(string='作物名称', required=True, tracking=True)
    latin_name = fields.Char(string='拉丁学名')
    display_name = fields.Char(string='显示名称', compute='_compute_display_name', store=True)
    crop_type = fields.Selection(
        selection=[
            ('vegetable', '蔬菜'),
            ('fruit', '果树'),
            ('flower', '花卉'),
            ('herb', '香草/药草'),
            ('other', '其他'),
        ],
        string='类别',
        default='vegetable',
        tracking=True,
    )
    climate_notes = fields.Text(string='气候与管理要点')
    growth_cycle = fields.Char(string='生长周期', help='如 60-80 天或多年生')
    care_notes = fields.Text(string='日常管理提示')
    active = fields.Boolean(default=True)
    color = fields.Integer('颜色', help='便于看板标记')

    @api.depends('name', 'latin_name')
    def _compute_display_name(self):
        for record in self:
            if record.latin_name:
                record.display_name = f"{record.name} ({record.latin_name})"
            else:
                record.display_name = record.name

